package com.showsu.editor.job;

import com.showsu.editor.entity.doo.User;
import com.showsu.editor.entity.po.TblUserPrivilege;
import com.showsu.editor.enums.UserPrivilegeEnum;
import com.showsu.editor.enums.VipLevelEnum;
import com.showsu.editor.service.IUserPrivilegeService;
import com.showsu.editor.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @Author: William
 * @Description: 每天凌晨刷新用户当日权益
 * @Date: Created in 5:05 下午 2024/9/18
 * @Modified By:
 */
@Component
@Slf4j
public class VipCountSchedule {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserPrivilegeService userPrivilegeService;

    /**
     * 任务启动入口
     */
    @Scheduled(cron = "0 0 0 * * ? ")
    public void execute() {
        log.info("每天刷新客户当日权益的任务，启动");
        List<User> userList = userService.queryUnExpiredVipUser();
        if (CollectionUtils.isEmpty(userList)) {
            log.info("每天刷新客户当日权益的任务，结束");
            return;
        }
        Map<Integer, VipLevelEnum> vipLevelEnumMap = VipLevelEnum.getVipLevelEnumMap();
        for (User user : userList) {
            VipLevelEnum vipLevelEnum = vipLevelEnumMap.get(user.getVipLevelCode());
            if (vipLevelEnum == null) {
                log.info("用户:{}的套餐:{}其对应的套餐权益不存在，查一查怎么回事");
                continue;
            }
            try {
                userPrivilegeService.updateUserPrivilege(user.getId(), UserPrivilegeEnum.XHSLIVE.getCode(), vipLevelEnum.getXhsLive());
                log.info("用户:{}的权益:{xhslive}次数更新为:{}", user.getId(), vipLevelEnum.getXhsLive());
                userPrivilegeService.updateUserPrivilege(user.getId(), UserPrivilegeEnum.XHSCOMMENTSLIVE.getCode(), vipLevelEnum.getXhsLive());
                log.info("用户:{}的权益:{xhsCommentsLive}次数更新为:{}", user.getId(), vipLevelEnum.getXhsCommentsLive());
                userPrivilegeService.updateUserPrivilege(user.getId(), UserPrivilegeEnum.PHOTOVIDEO.getCode(), vipLevelEnum.getPhotoVideo());
                log.info("用户:{}的权益:{photovideo}次数更新为:{}", user.getId(), vipLevelEnum.getPhotoVideo());
                userPrivilegeService.updateUserPrivilege(user.getId(), UserPrivilegeEnum.GIFPHOTO.getCode(), vipLevelEnum.getGifPhoto());
                log.info("用户:{}的权益:{gifphoto}次数更新为:{}", user.getId(), vipLevelEnum.getGifPhoto());
                userPrivilegeService.updateUserPrivilege(user.getId(), UserPrivilegeEnum.COMMENTPHOTO.getCode(), vipLevelEnum.getCommentPhoto());
                log.info("用户:{}的权益:{commentphoto}次数更新为:{}", user.getId(), vipLevelEnum.getCommentPhoto());
                userPrivilegeService.updateUserPrivilege(user.getId(), UserPrivilegeEnum.AI_CHECKTEXT.getCode(), vipLevelEnum.getAiCheckText());
                log.info("用户:{}的权益:{aiCheckText}次数更新为:{}", user.getId(), vipLevelEnum.getAiCheckText());
                userPrivilegeService.updateUserPrivilege(user.getId(), UserPrivilegeEnum.XHS_AWEME_INCLUDED.getCode(), vipLevelEnum.getXhsAwemeIncluded());
                log.info("用户:{}的权益:{aiCheckText}次数更新为:{}", user.getId(), vipLevelEnum.getAiCheckText());
            } catch (Exception e) {
                continue;
            }
        }
        log.info("每天刷新客户当日权益的任务，结束");
    }
}
